package com.hfi.restful.wiremock;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

/**
 * @author ChangLiang
 * @date 2019/8/1
 */
public class WireMockServer {

    public static void main(String[] args) throws IOException {
        configureFor("192.168.211.101",8062);
        removeAllMappings();
//        mock("/order/1", "01");
//        mock("/order/2", "02");
        mock("/authorize/list", "immocmanager_authority_list");
    }

    private static void mock(String url, String file) throws IOException {
        // 注意在建package的时候要一层一层地建
        ClassPathResource resource = new ClassPathResource("mock/response/" + file + ".txt");
        // 使用StringUtils.join 将文本内容转换为一行的json字符串
        String content = StringUtils.join(FileUtils.readLines(resource.getFile(), "UTF-8").toArray(), "\n");
        // stub 桩
        // 对url发出get请求 将会return 200 with content body
        stubFor(get(urlPathEqualTo(url)).willReturn(aResponse().withBody(content).withHeader("Content-Type",
                "application/json").withStatus(200)));
    }
}
